<?php
namespace Admin\Controller;
use Think\Controller;
class SystemController extends Controller {
    /**
     * 系统角色
     */
    public function systemRole(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $role = M('role');
        if($admin['user_role'] ==5 ){
            $data = $role->select();
        }else{
            $data = $role->where("role_id <> 5")->select();
        }

        $this->assign('data',$data);
        $this->display();
    }
    //角色添加
    public function role_add(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}

        $this->display();
    }
    //角色添加数据接收
    public function role_date(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}

        $role = M('role');
        $date['role_name']  =  I('post.role_name')?I('post.role_name'):die('不能为空');
        $date['role_desp']  =  I('post.role_desp')?I('post.role_desp'):die("不能为空");

        if(empty($date['role_name']) ||  empty($date['role_desp'])){$this->ajaxReturn(array('error'=>"不能有空",'status'=>0));exit;}
        $query = M('role')->where(array('role_name'=>$date['role_name']))->find();
        if(!empty($query)){$this->ajaxReturn(array('error'=>"角色名已存在",'status'=>0));  exit;}

        $role_add = $role->add($date);
        if($role_add != false){
            $this->ajaxReturn(array('msg'=>"添加成功",'status'=>1));
        }else{
            $this->ajaxReturn(array('error'=>"添加失败",'status'=>0));
        }
    }
    //角色删除
    public function admin_role_del(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $role_del = M('role');
        $id['role_id'] = I('id');

        $del = $role_del->where($id)->delete();
        if ($del) {
            $this->ajaxReturn(array('msg'=>"删除成功",'status'=>1));
        } else {
            $this->ajaxReturn(array('error'=>"删除失败",'status'=>0));
        }

    }
    //角色修改+权限修改页面
    public function role_save(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $role_id['role_id'] = I('get.id');
        $role  = M('role');
        $role_one = $role->where($role_id)->find();

        if($admin['user_role'] < 4){
            //根据传入ID查询当前角色拥有的权限
            $role_date = $role->where($role_id)->join('interaction on interaction.inter_pid = role.role_id')->select();
            $menu_id=[];
            foreach($role_date as $val){
                $menu_id[].=$val['inter_mid'];
            }
            $menu_list = M('menu')->where("menu_parent = 0")->select();//一级
            $menu_tow = M('menu')->where("menu_parent <> 0")->select(); //二级

        }else{
            //根据传入ID查询当前角色拥有的权限
            $role_date = $role->where($role_id)->join('interaction on interaction.inter_pid = role.role_id')->select();
            $menu_id=[];
            foreach($role_date as $val){
                $menu_id[].=$val['inter_mid'];
            }
            $menu_list = M('menu')->where("menu_parent = 0  and menu_id !=84")->select();//一级
            $menu_tow = M('menu')->where("menu_parent <> 0")->select();                  //二级
        }


        $this->assign('mu',$menu_id);
        $this->assign('role_one',$role_one);            //角色信息
        $this->assign('role_date',$role_date);          //角色现有权限
        $this->assign('menu_list',$menu_list);          //权限菜单父级
        $this->assign('menu_tow',$menu_tow);            //权限菜单子级
        $this->display();
    }
    //权限修改数据写入
    public function Role_SaveData(){
        $admin =session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id')?I('id'):die("信息错误,请重试");
        $data['role_name'] = I('role_name')? I("role_name"):die("角色名不可为空");
        $data['role_desp'] = I('role_desp')? I("role_desp"):die("角色名描述不能为空");
        $checkbox = I('post.checkbox')?I('post.checkbox'):die("权限选择不能为空");
        $checkbox = explode(",", $checkbox);
        $role_save = M('role')->where(array('role_id'=>$id))->save($data);
        if($role_save!==false){
            $interaction = M('interaction');
            $past_role = $interaction->field('inter_mid')->where(array('inter_pid'=>$id))->select();
            $m_id=[];
            foreach($past_role as $val){
                $m_id[].=$val['inter_mid'];
            }
            if(!$past_role) {        //如果数据库中原有权限查询为空，则直接写入传过来的数据
                foreach ($checkbox as $val) {
                    $data['inter_pid'] = $id;
                    $data['inter_mid'] = $val;
                    $interaction->add($data);
                }
                $this->ajaxReturn(array('msg' => "修改成功", 'status' => 1));
            }else {
                //如果数据库中存在的权限，不在传过来的权限之中，表示该权限已被删除
                foreach($m_id as $k){
                    if(!in_array($k,$checkbox)){
                        $interaction->where("inter_mid=$k AND inter_pid=$id")->delete();
                    }
                }
                //如果传过来的权限不存在数据库中，则表明此权限为新增
                foreach($checkbox as $val){
                    if(!in_array($val,$m_id)){
                        $data['inter_pid'] = $id;
                        $data['inter_mid'] = $val;
                        $interaction->add($data);
                    }
                }
                $this->ajaxReturn(array('msg' =>"修改成功",'status' =>1));
            }

        }else{
            $this->ajaxReturn(array('error' =>"修改失败",'status' =>0));
        }

    }
    //角色修改 + 权限写入 旧的
   /* public function Role_DateSave(){
        $admin = array('user_id'=>1);//session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $role   = I('id'); //传入当前点击进入的角色ID
        //修改角色信息
        $role_up =  M('role');
        $role_date['role_name'] = I('post.role_name');
        $role_date['role_desp'] = I('post.role_desp');
        $rs = $role_up->where("role_id = $role")->save($role_date);

        if($rs!==false) {  //如果角色信息修改成功就进行权限修改
            //权限信息修改
            $role_add_date = I('post.checkbox');
            $power = M('Interaction');
            //数据库中原有的权限
            $power_rs  =  $power->field('inter_mid')->where("inter_pid = $role")->select();
            $m_id=[];
            foreach($power_rs as $val){
                $m_id[].=$val['inter_mid'];
            }
            if(!$power_rs) {        //如果数据库中原有权限查询为空，则直接写入传过来的数据
                foreach($role_add_date as $val){
                    $data['inter_pid'] = $role;
                    $data['inter_mid'] = $val;
                    $power->add($data);
                }
                $this->success('修改成功',$this->site_url,1);

            } else {        //如果数据库中存在的权限，不在传过来的权限之中，表示该权限已被删除
                foreach($m_id as $wc){
                    if(!in_array($wc,$role_add_date)){
                        $power->where("inter_mid=$wc AND inter_pid=$role")->delete();
                    }
                }
                //如果传过来的权限不存在数据库中，则表明此权限为新增
                foreach($role_add_date as $val){
                    if(!in_array($val,$m_id)){
                        $data['inter_pid'] = $role;
                        $data['inter_mid'] = $val;
                        $power->add($data);
                    }
                }
                $this->success('修改成功',$this->site_url,1);
            }

        }else {
            $this->error('数据修改失败',$this->site_url,1);
        }
    }
*/
    /**
     * 组别管理
     */
    public function group(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $data = M('mt4_groups')->select();
        foreach ($data as $k=>$v){
            if($v['type']==1){
                $data[$k]['type'] = "短期";
            }else{
                $data[$k]['type'] = "外汇";
            }
        }

        $this->assign('data',$data);
        $this->display();
    }
    //组别添加
    public function group_add(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $data['mt4_group'] = I('post.mt4_group')?I('post.mt4_group'):'';
        $data['type'] = I('post.type');
        $data['ratio'] = I('post.ratio')?I('post.ratio'):'0';
        $data['enable_read_only'] = 0;
        if(empty($data['mt4_group'])){
            $this->display();
        }else{
            if(empty($data['mt4_group']) || empty($data['type'])){
                $this->ajaxReturn(array('msg'=>"组别名不可为空",'status'=>0));
            }
           $query = M('mt4_groups')->where(array('mt4_group'=>$data['mt4_group']))->find();
            if(empty($query)){
                $add = M('mt4_groups')->add($data);
                if($add){
                    $this->ajaxReturn(array('msg'=>"添加成功",'status'=>1));
                }else{
                    $this->ajaxReturn(array('msg'=>"添加失败",'status'=>0));
                }
            }else{
                $this->ajaxReturn(array('msg'=>"组别名已存在",'status'=>0));
            }
        }
    }
    //组别修改
    public function group_save(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id');
        if(empty($id)){echo "无效操作";exit;}

        $data = M('mt4_groups')->where(array('id'=>$id))->find();

        $this->assign('data',$data);
        $this->display();
    }
    //修改
    public function group_saveData(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id  = I('id');
        if(empty($id)){$this->ajaxReturn(array('error'=>"数据无效",'status'=>0));}
        $mt4_group = I('mt4_group')?I('mt4_group'):die("组别不能为空");
        $ratio     = I('post.ratio')?I('post.ratio'):'0';
        $type      = I('type')?I('type'):'1';

        $query = M('mt4_groups')->where(array('mt4_group'=>$mt4_group))->find();
        if(empty($query)){
            $data['mt4_group']  = $mt4_group;
            $data['ratio']      = $ratio;
            $data['type']       = $type;
        }else{
            $data['ratio']      = $ratio;
            $data['type']       = $type;
        }
        $save = M('mt4_groups')->where(array('id'=>$id))->save($data);
        if($save!==false){
            $this->ajaxReturn(array('msg'=>"修改成功",'status'=>1));
        }else{
            $this->ajaxReturn(array('error'=>"修改失败",'status'=>0));
        }
    }
    //组别删除
    public function group_delete(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id');
        if(empty($id)){$this->ajaxReturn(array('信息错误，请刷新再试'));exit;}
        $delete = M('mt4_groups')->where(array('id'=>$id))->delete();
        if($delete){
            $this->ajaxReturn(array('msg'=>"删除成功",'status'=>1));
        }else{
            $this->ajaxReturn(array('error'=>"删除失败",'status'=>0));
        }
    }
    //批量只读无交易
    public function updateEnableReadOnly(){
        $group=I('post.group') ? I('post.group') : die(json_encode(array('status'=>0,'msg'=>'请选择组别')));
        $tradingblocked=I('post.enable_read_only') ? I('post.enable_read_only') : 0;
        $users=M('user')->field('user_id')->where(['group'=>$group])->select();
        if(empty($users)){
            $this->ajaxReturn(array('status'=>0,'msg'=>'组别没人'));
        }
        $login=[];
        foreach($users as $k => $v){
            $login[]=$v['user_id'];
        }
        $model=M('mt4_groups');
        $model->startTrans();
        $res=$model->where(['mt4_group'=>$group])->save(['enable_read_only'=>$tradingblocked]);
        if($res!==false){
            $login=implode(';',$login);
            $request = "action=bulkmodifyaccount&login=".$login."&enable_read_only=".$tradingblocked."\0";
            $return=mt4_operation($request);
            parse_str($return,$res1);
            if($res1['result']==1){
                $model->commit();
                $this->ajaxReturn(array('status'=>1,'msg'=>'操作成功'));
            }else{
                $model->rollback();
                $this->ajaxReturn(array('status'=>0,'msg'=>'操作失败'));
            }
        }else{
            $this->ajaxReturn(array('status'=>0,'msg'=>'操作失败'));
        }
    }
    /**
     *
     * 职位管理
     */
    //职位列表
    public function position(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $data = M('position')->select();
        $this->assign('data',$data);
        $this->display();
    }
    //职位添加
    public function position_add(){
        $admin = array('user_id'=>1);//session('admin');
        if(empty($admin)){$this->ajaxReturn(array('msg'=>"身份信息过期请重新登录",'status'=>0));}
        $data['user_position'] = I('user_position');
        if(empty($data['user_position'])){
            $this->display();
        }else {
            $query = M('position')->where(array('user_position'=>$data['user_position']))->find();
            if(empty($query)){
                $data['depict'] = I('post.depict')?I('post.depict'):"";
                $data['ratio'] = I('post.ratio')?I('post.ratio'):0;
                $data['ratio_foreign'] = I('post.ratio_foreign')?I('post.ratio_foreign'):0;
                $add = M('position')->add($data);
                if($add){
                    $this->ajaxReturn(array('msg'=>'添加成功','status'=>1));
                }else{
                    $this->ajaxReturn(array('msg'=>'添加成功','status'=>0));
                }
            }else{
                $this->ajaxReturn(array('msg'=>'职位名已存在','status'=>0));
            }
        }
    }
    //职位修改
    public function position_save(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id');
        if(empty($id)){
            echo "没有数据，请重试";exit;
        }
        $data = M('position')->where(['id'=>$id])->find();
        if(empty($data)){
            echo "没有数据，请重试";exit;
        }else {
            $this->assign('data',$data);
            $this->display();
        }
    }
    //修改数据
    public function position_saveData(){
        $admin = session('admin');
        if(empty($admin)){$this->ajaxReturn(array('msg'=>"身份信息过期请重新登录",'status'=>0));}
       $id = I('id');
       if(empty($id)){
           $this->ajaxReturn(array('msg'=>"操作错误，请刷新页面再试",'status'=>0));
       }
       $data['user_position'] = I('post.user_position')?I('post.user_position'):die(json_encode(array('msg'=>"职位名不能为空",'state'=>0)));
       $data['depict'] = I('post.depict')?I('post.depict'):"";
       $data['ratio'] = I('post.ratio')?I('post.ratio'):0;
       $data['ratio_foreign'] = I('post.ratio_foreign')?I('post.ratio_foreign'):0;
       if(empty($data['user_position'])){
           $this->ajaxReturn(array('msg'=>"职位名不能为空",'status'=>0));
       }else{
           $save = M('position')->where(['id'=>$id])->save($data);
           if($save!==false){
               $this->ajaxReturn(array('msg'=>"修改成功",'status'=>1));
           }else{
               $this->ajaxReturn(array('msg'=>"修改失败",'status'=>0));
           }
       }

    }
    //删除
    public function position_delete(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id');
        if(empty($id)){
            $this->ajaxReturn(array('error'=>"操作错误，请刷新页面再试",'status'=>0));exit;
        }
        $delete = M('position')->where(['id'=>$id])->delete();
        if($delete){
            $this->ajaxReturn(array('msg'=>"删除成功",'status'=>1));
        }else{
            $this->ajaxReturn(array('msg'=>"删除失败",'status'=>0));
        }
    }

    /**
     * 邮箱设置
     */
    public function emailData(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $data = M('send_email')->find();

        $this->assign('data',$data);
        $this->display();
    }
    public function email_save(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id');
        if(empty($id)){
            echo "非法操作";exit;
        }else{
            $data = M('send_email')->where(['id'=>$id])->find();
            $this->assign('data',$data);
            $this->display();
        }
    }
    public function email_saveData(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id');
        if(empty($id)){
            $this->ajaxReturn(array('error'=>'错误的操作','status'=>0)); exit;
        }

        $data['adds'] = I('post.adds')?I('post.adds'):die($this->ajaxReturn(['status'=>0,'msg'=>'请输入邮箱地址']));
        $data['name'] = I('post.name')?I('post.name'):die($this->ajaxReturn(['status'=>0,'msg'=>'请输入邮箱账号']));
        $data['smtp'] = I('post.smtp')?I('post.smtp'):die($this->ajaxReturn(['status'=>0,'msg'=>'请输入邮箱smtp服务地址']));
        $data['password'] = I('post.password')?I('post.password'):die($this->ajaxReturn(['status'=>0,'msg'=>'请输入邮箱发送密码']));
        $data['sender'] = I('post.sender')?I('post.sender'):die($this->ajaxReturn(['status'=>0,'msg'=>'请输入发件人名称']));
        if(empty($data['adds']) || empty($data['name']) || empty($data['smtp']) || empty($data['password']) || empty($data['sender'])){
            $this->ajaxReturn(array('error'=>'信息填写不够完整','status'=>0));exit;
        }else{
            $save = M('send_email')->where(['id'=>$id])->save($data);
            if($save!==false){
                $this->ajaxReturn(array('msg'=>'修改成功','status'=>1));
            }else{
                $this->ajaxReturn(array('error'=>'修改失败','status'=>0));
            }
        }
    }

    /**
     * 邮件模板
     */
    public function emailModel(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $count = M('mail_mould')->count();
        $Page = new \Think\Page($count, 20);
        $show = $Page->show();// 分页显示输出
        $data = M('mail_mould')->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach($data as $k=>$v){
            $data[$k]['time']=date('Y-m-d H:i:s',$v['time']);
        }

        $this->assign('data',$data);
        $this->assign('page',$show);
        $this->display();
    }
    //模板添加
    public function emailModel_add(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $value = I('post.');
        if(empty($value)){
            $this->display();
        }else{
           $data['title'] = I('post.title')? I('post.title') :die($this->ajaxReturn(['status'=>0,'error'=>'请输入邮件标题']));
           $data['content'] = I('post.content')? I('post.content') :die($this->ajaxReturn(['status'=>0,'error'=>'请输入邮件内容']));
           $data['type'] = I('post.type')? I('post.type') :die($this->ajaxReturn(['status'=>0,'error'=>'请输入邮件编号']));
           $data['time'] = time();
          $query = M('mail_mould')->where(['type'=>$data['type']])->find();
          if(!empty($query)){
              $this->ajaxReturn(array('error'=>"邮件编号已存在",'status'=>0));exit;
          }else{
              $add = M('mail_mould')->add($data);
              if($add){
                  $this->ajaxReturn(array('msg'=>"添加成功",'status'=>1));
              }else{
                  $this->ajaxReturn(array('error'=>"添加失败",'status'=>0));
              }
          }
        }
    }
    //修改
    public function emailModel_save(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id');
        if(empty($id)){
            echo "非法操作";exit;
        }else{
            $data = M('mail_mould')->where(['id'=>$id])->find();
            $this->assign('data',$data);
            $this->display();
        }
    }
    //修改数据
    public function emailModel_saveData(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id');
        if(empty($id)){
            $this->ajaxReturn(array('error'=>"error.404",'status'=>0));exit;
        }
        $data['title'] = I('post.title')? I('post.title') :die($this->ajaxReturn(['status'=>0,'error'=>'请输入邮件标题']));
        $data['content'] = I('post.content')? I('post.content') :die($this->ajaxReturn(['status'=>0,'error'=>'请输入邮件内容']));
        $data['type'] = I('post.type')? I('post.type') :die($this->ajaxReturn(['status'=>0,'error'=>'请输入邮件编号']));

        if(empty($data['title'])||empty($data['content'])||empty($data['type'])){
            $this->ajaxReturn(array('error'=>"请确保所以信息完整",'status'=>0)); exit;
        }else{
            $query = M('mail_mould')->where(['type'=> $data['type']])->find();
            if(empty($query)){
                $save = M('mail_mould')->where(['id'=>$id])->save($data);
            }else{
                $save = M('mail_mould')->where(['id'=>$id])->save(array('title'=>$data['title'],'content'=>$data['content']));
            }
            if($save!==false){
                $this->ajaxReturn(array('msg'=>"修改成功",'status'=>1));
            }else{
                $this->ajaxReturn(array('error'=>"修改失败",'status'=>0));
            }
        }
    }
    //删除
    public function emailModel_delete(){
        $admin =session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id');
        if(empty($id)){
            $this->ajaxReturn(array('error'=>"error.404",'status'=>0)); exit;
        }else{
            $de = M('mail_mould')->where(['id'=>$id])->delete();
            if($de){
                $this->ajaxReturn(array('msg'=>"删除成功",'status'=>1));
            }else{
                $this->ajaxReturn(array('error'=>"删除失败",'status'=>0));
            }
        }

    }

    /**
     * 权限节点
     */
    //权限列表
    public function power(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $count = M('menu')->count();
        $Page = new \Think\Page($count, 20);
        $show = $Page->show();
        $data = M('menu')->limit($Page->firstRow.','.$Page->listRows)->select();
        $main_menu = M('menu')->where(['menu_parent'=>0])->select();

        foreach ($data as $k=>$v){
            foreach ($main_menu as $ke =>$val){
                if($v['menu_parent']==$val['menu_id']){
                    $data[$k]['menu_parent']=$val['menu_name'];
                }
            }
            if($v['menu_parent']==0){
                $data[$k]['menu_parent'] ="<font color='green'>主节点</font>";
            }
        }

        //var_dump($admin);die;
        $this->assign('admin',$admin);
        $this->assign('main_menu',$main_menu);
        $this->assign('page',$show);
        $this->assign('data',$data);
        $this->display();
    }
    //开关菜单
    public function switch_offON(){
        $switch = I('state')?I('state'):die($this->ajaxReturn(array('error'=>"失败",'status'=>0)));
        $id = I('id')?I('id'):die($this->ajaxReturn(array('error'=>"失败",'status'=>0)));
        $save = M('menu')->where(['menu_id'=>$id])->save(array('switch'=>$switch));
        if($save!=false){
            $this->ajaxReturn(array('msg'=>"修改成功",'status'=>1));
        }else{
            $this->ajaxReturn(array('error'=>"修改失败",'status'=>0));
        }
    }
    //添加
    public function power_add(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        if(empty($_POST)){
            $main_menu = M('menu')->where(['menu_parent'=>0])->select();
            $this->assign('main_menu',$main_menu);
            $this->display();
        }else{
            $data['menu_name'] = I('menu_name')?I('menu_name'):die($this->ajaxReturn(array('error'=>"菜单名不能为空",'status'=>0)));
            $data['menu_href'] = I('menu_href')?I('menu_href'):die($this->ajaxReturn(array('error'=>"菜单链接不能为空",'status'=>0)));
            $data['menu_parent'] = I('menu_parent')?I('menu_parent'):0;
            $data['class'] = I('class')?I('class'):'';

            $add = M('menu')->add($data);
            if($add){
                $this->ajaxReturn(array('msg'=>"添加成功",'status'=>1));
            }else{
                $this->ajaxReturn(array('error'=>"添加失败",'status'=>0));
            }
        }

    }
    //修改
    public function power_save(){
        $admin =session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('menu_id');
        if(empty($id)){
            echo "error.404"; exit;
        }
        $main_menu = M('menu')->where(['menu_parent'=>0])->select();
        $data = M('menu')->where(array('menu_id'=>$id))->find();



        $this->assign('main_menu',$main_menu);
        $this->assign('data',$data);
        $this->display();
    }
    public function power_saveData(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('menu_id');
        if(empty($id)){
            $this->ajaxReturn(array('error'=>"error.404",'status'=>0)); exit;
        }
        $data['menu_name'] = I('menu_name')?I('menu_name'):die($this->ajaxReturn(array('error'=>"菜单名不能为空",'status'=>0)));
        $data['menu_href'] = I('menu_href')?I('menu_href'):die($this->ajaxReturn(array('error'=>"菜单链接不能为空",'status'=>0)));
        $data['menu_parent'] = I('menu_parent')?I('menu_parent'):0;

        $save =M('menu')->where(['menu_id'=>$id])->save($data);
        if($save!==false){
            $this->ajaxReturn(array('msg'=>"修改成功",'status'=>1));
        }else{
            $this->ajaxReturn(array('error'=>"修改失败",'status'=>0));
        }
    }
    //cahxun
    public function menu_parent(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('menu_id');
        if(empty($id)){
            echo "404";exit;
        }
        $data = M('menu')->where(['menu_id'=>$id])->find();
        if($data['menu_parent']==0){
            $this->ajaxReturn(['status'=>1]);
        }else{
            $this->ajaxReturn(['status'=>0]);
        }

    }
    //删除菜单
    public function power_delete(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('menu_id');
        if(empty($id)){
            $this->ajaxReturn(array('error'=>"error.404",'status'=>0)); exit;
        }
        $data = M('menu')->where(['menu_id'=>$id])->find();
        //当删除的为主菜单时将删除其下面的所有子菜单
        if($data['menu_parent']==0){
            $where['menu_id'] = $id;
            $where['menu_parent'] = $id;
            $where['_logic'] = 'OR';
           $delete = M('menu')->where($where)->delete();
           if(empty($delete)){
                $this->ajaxReturn(array('error'=>"删除失败",'status'=>0));
            }else{
                $this->ajaxReturn(array('msg'=>"删除成功",'status'=>1));
            }

        }else{//反之
            $de = M('menu')->where(['menu_id'=>$id])->delete();
            if($de){
                $this->ajaxReturn(array('msg'=>"删除成功",'status'=>1));
            }else{
                $this->ajaxReturn(array('error'=>"删除失败",'status'=>0));
            }
        }
    }

    /**
     * 入金设置
     *
     */
    public function Into_GoldSet(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $data =M('into_gold_set')->select();
        foreach ($data as $k=>$v){
            if($v['state']==1){
                $data[$k]['state']="<font color='green'>已启用</font>";
            }else{
                $data[$k]['state']="<font color='red'>已关闭</font>";
            }
            if($v['type']==1){
                $data[$k]['type']="<font color='green'>购买汇率</font>";
            }else{
                $data[$k]['type']="<font color='red'>收益比例</font>";
            }

        }
        $this->assign('data',$data);
        $this->display();
    }
    //添加
    public function Into_GoldSetAdd(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $msg=I('');
        if(empty($msg)){
            $this->display();
        }else{
            $data['name'] = I('post.name')?I('post.name'):die("名称不能为空");
            $data['minimum'] = I('post.minimum')?I('post.minimum'):'0';
            $data['fee'] = I('post.fee')?I('post.fee'):'0';
            $data['max_fee'] = I('post.max_fee')?I('post.max_fee'):'0';
            $data['rate'] = I('post.rate')?I('post.rate'):die("汇率不能为空");
            $data['state'] = I('post.state')?I('post.state'):'2';
            $data['type'] = I('post.type')?I('post.type'):'1';
            $data['remarks'] = I('post.remarks')?I('post.remarks'):die("说明不能为空");
            $data['partyid'] = I('post.partyid')?I('post.partyid'):die("商户编号不能为空");
            $data['returnurl'] = I('post.returnurl')?I('post.returnurl'):die("返回地址不能为空");
            $data['encodetype'] = I('post.encodetype')?I('post.encodetype'):die("验证不能为空");
            $data['md5key'] = I('post.md5key')?I('post.md5key'):die("md5key不能为空");
            $add = M('into_gold_set')->add($data);
            if($add){
                //$this->ajaxReturn(array('msg'=>"添加成功",'status'=>1));
                echo "添加成功";

            }else{
                //$this->ajaxReturn(array('error'=>"添加失败",'status'=>0));
                echo "添加失败";
            }
        }
    }
    //修改
    public function Into_GoldSetSave(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id')?I('id'):$this->ajaxReturn(array('error'=>"非法操作",'status'=>0));
        $data = M('into_gold_set')->where(['id'=>$id])->find();
        if(!empty($data)){
            $this->assign('data',$data);
            $this->display();
        }else{
            if(empty($id)){$this->ajaxReturn(array('error'=>"无数据信息，请重试",'status'=>0));exit;}
        }
    }
    //修改数据
    public function Into_GoldSetSaveData(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('get.id');
        if(empty($id)){echo "无数据信息，请重试";exit;} //$this->ajaxReturn(array('error'=>"无数据信息，请重试",'status'=>0));
        $data['name'] = I('post.name')?I('post.name'):die("名称不能为空");
        $data['minimum'] = I('post.minimum')?I('post.minimum'):'0';
        $data['fee'] = I('post.fee')?I('post.fee'):'0';
        $data['max_fee'] = I('post.max_fee')?I('post.max_fee'):'0';
        $data['rate'] = I('post.rate')?I('post.rate'):die("汇率不能为空");
        $data['state'] = I('post.state')?I('post.state'):'2';
        $data['type'] = I('post.type')?I('post.type'):'1';
        $data['remarks'] = I('post.remarks')?I('post.remarks'):die("说明不能为空");
        $data['partyid'] = I('post.partyid')?I('post.partyid'):'';
        $data['returnurl'] = I('post.returnurl')?I('post.returnurl'):'';
        $data['encodetype'] = I('post.encodetype')?I('post.encodetype'):'';
        $data['md5key'] = I('post.md5key')?I('post.md5key'):'';
        $save = M('into_gold_set')->where(['id'=>$id])->save($data);
        if($save!==false){
            //$this->ajaxReturn(array('msg'=>"修改成功",'status'=>1));
            echo "修改成功";
        }else{
            //$this->ajaxReturn(array('error'=>"修改失败",'status'=>0));
            echo "修改失败";
        }
    }
    //删除
    public function Into_GoldSetDelete(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id');
        if(empty($id)){$this->ajaxReturn(array('error'=>"无数据信息，请重试",'status'=>0));exit;}
        $de = M('into_gold_set')->where(['id'=>$id])->delete();
        if($de){
            $this->ajaxReturn(array('msg'=>"删除成功",'status'=>1));
        }else{
            $this->ajaxReturn(array('error'=>"删除失败",'status'=>0));
        }

    }

    /**
     * 出金设置
     *
     */
    public function Gold_Sst(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $data = M('gold_set')->select();
        foreach ($data as $k=>$v){
            if($v['state']==1){
                $data[$k]['state']="<font color='green'>已启用</font>";
            }else{
                $data[$k]['state']="<font color='red'>未启用</font>";
            }
        }
        $this->assign('data',$data);
        $this->display();
    }
    //添加
    public function GoldSet_Add(){
        $admin =session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $msg=I('');
        if(empty($msg)){
            $this->display('Gold_Add');
        }else{
            $data['ch_name'] = I('ch_name')? I('ch_name'):die('名字不能为空');
            $data['en_name'] = I('en_name')? I('en_name'):die('英文名不能为空');
            $data['max_into'] = I('max_into')? I('max_into'):'0';
            $data['min_into'] = I('min_into')? I('min_into'):'0';
            $data['rate'] = I('rate')? I('rate'):die('汇率不能为空');
            $data['fee'] = I('fee')? I('fee'):'0';
            $data['max_fee'] = I('max_fee')? I('max_fee'):'0';
            $data['remarks'] = I('remarks')? I('remarks'):die('说明不能为空');
            $data['state'] = I('state')? I('state'):'1';
            $add =M('gold_set')->add($data);
            if($add){
                echo "添加成功";
            }else{
                echo "添加失败";
            }
        }
    }
    //修改
    public function GoldSet_Save(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id');
        if(empty($id)){exit;}
        $data = M('gold_set')->where(['id'=>$id])->find();
        if(!empty($data)){

            $this->assign('data',$data);
            $this->display('Gold_Save');
        }
    }
    public function GoldSet_SaveData(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('get.id');
        if(empty($id)){echo "无效的操作";exit;}
        $data['ch_name'] = I('ch_name')? I('ch_name'):die('名字不能为空');
        $data['en_name'] = I('en_name')? I('en_name'):die('英文名不能为空');
        $data['max_into'] = I('max_into')? I('max_into'):'0';
        $data['min_into'] = I('min_into')? I('min_into'):'0';
        $data['rate'] = I('rate')? I('rate'):die('汇率不能为空');
        $data['fee'] = I('fee')? I('fee'):'0';
        $data['max_fee'] = I('max_fee')? I('max_fee'):'0';
        $data['remarks'] = I('remarks')? I('remarks'):die('说明不能为空');
        $data['state'] = I('state')? I('state'):'1';
        $save = M('gold_set')->where(['id'=>$id])->save($data);
        if($save!==false){
            echo "修改成功";
        }else{
            echo "修改失败";
        }
    }
    //删除
    public function GoldSet_Delete(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id =I('id');
        if(empty($id)){$this->ajaxReturn(array('error'=>"无效的操作",'status'=>0));exit;}
        $de = M('gold_set')->where(['id'=>$id])->delete();
        if($de){
            $this->ajaxReturn(array('msg'=>"删除成功",'status'=>1));
        }else{
            $this->ajaxReturn(array('error'=>"删除失败",'status'=>0));
        }
    }

    public function symbolSpread(){//品种点差
        $list=M('symbol_spread')->field('id,symbol,spread')->order('id desc')->select();
        $this->assign('list',$list);
        $this->display();
    }



    public function addSymbolSpreadView(){//添加品种点差页面
        $this->display();
    }

    public function addSymbolSpread(){//添加品种点差
        $data['symbol']=I('post.symbol') ? I('post.symbol') : die(json_encode(array('msg'=>"请设置交易品种",'status'=>0)));
        $data['spread']=I('post.spread') ? I('post.spread') : die(json_encode(array('msg'=>"请设置点差值",'status'=>0)));
        $model=M('symbol_spread');
        $has=$model->where(['symbol'=>$data['symbol']])->find();
        if(!empty($has)){
            $this->ajaxReturn(array('msg'=>"该品种已存在",'status'=>0));
        }
        $res=M('symbol_spread')->add($data);
        if($res){
            $this->ajaxReturn(array('msg'=>"操作成功",'status'=>1));
        }else{
            $this->ajaxReturn(array('msg'=>"操作失败",'status'=>0));
        }
    }

    public function deleteSymbolSpread(){//删除品种点差
        $id=I('post.id') ? I('post.id') : die(json_encode(array('msg'=>"非法操作",'status'=>0)));
        $res=M('symbol_spread')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('msg'=>"操作成功",'status'=>1));
        }else{
            $this->ajaxReturn(array('msg'=>"操作失败",'status'=>0));
        }
    }

    public function updateSymbolSpreadView(){//修改品种点差页面
        $id=I('id') ? I('id') : die('非法操作，<a href="javascript:history.back(-1)">点击返回</a>');
        $msg=M('symbol_spread')->where(['id'=>$id])->field('id,symbol,spread')->find();
        $this->assign('msg',$msg);
        $this->display();
    }

    public function updateSymbolSpread(){//修改品种点差
        $id=I('post.id') ? I('post.id') : die(json_encode(array('msg'=>"非法操作",'status'=>0)));
        $data['symbol']=I('post.symbol') ? I('post.symbol') : die(json_encode(array('msg'=>"请设置交易品种",'status'=>0)));
        $data['spread']=I('post.spread') ? I('post.spread') : die(json_encode(array('msg'=>"请设置点差值",'status'=>0)));
        $res=M('symbol_spread')->where(['id'=>$id])->save($data);
        if($res){
            $this->ajaxReturn(array('msg'=>"操作成功",'status'=>1));
        }else{
            $this->ajaxReturn(array('msg'=>"操作失败",'status'=>0));
        }
    }


}
